package com.pe.pgn.clubpgn.webapp.controller.DocumentoPago;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpmDocumentoPagoEmpresa;
import com.pe.pgn.clubpgn.domain.ClpbClienteEmpresa;
import com.pe.pgn.clubpgn.domain.beans.BNClienteEmpresa;
import com.pe.pgn.clubpgn.service.ClienteEmpresaService;
import com.pe.pgn.clubpgn.service.DocumentoPagoEmpresaService;
import com.pe.pgn.clubpgn.service.SincronizacionFlotaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;
import com.pe.pgn.clubpgn.webapp.util.CalendarUtil;

@Controller
@RequestMapping("/documentopago/**")
public class DocumentoPagoController extends BaseFormController {

	@Autowired
	private DocumentoPagoEmpresaService documentoPagoEmpresaService;
	
	@Autowired
	private ClienteEmpresaService clienteEmpresaService;
	
	@Autowired
	private SincronizacionFlotaService sincronizacionService;
	
	@RequestMapping(value = "/documentopagolist.html")
    public @ModelAttribute("documentoPago")ClpmDocumentoPagoEmpresa buscarDocumentoPago(
    			@ModelAttribute("documentoPago") ClpmDocumentoPagoEmpresa documentoPago,
    			@RequestParam(value = "search", required = false) String search, Model model) {
		
		if(StringUtils.isBlank(search)){
			List<ClpmDocumentoPagoEmpresa> documentoPagos = documentoPagoEmpresaService.obtenerDocumentoPagoEmpresas(documentoPago, Boolean.TRUE) ;
			model.addAttribute("documentoPagos", documentoPagos);			
			return new ClpmDocumentoPagoEmpresa();
		}		
		List<ClpmDocumentoPagoEmpresa> documentoPagos = documentoPagoEmpresaService.obtenerDocumentoPagoEmpresas(documentoPago, Boolean.FALSE); 
		model.addAttribute("documentoPagos", documentoPagos);		
		return documentoPago;
    }		
	
	@RequestMapping(value = "/documentopagoform.html", method = RequestMethod.GET)
	public @ModelAttribute("documentoPago")  ClpmDocumentoPagoEmpresa verDocumentoPago(
				@RequestParam(value = "id", required = false) Long id, Model model) {
		
 		if (id != null) {
			ClpmDocumentoPagoEmpresa documentoPago = new ClpmDocumentoPagoEmpresa();//OBTENER AQUI		
			documentoPago = documentoPagoEmpresaService.obtenerDocumentoPagoEmpresa(id);
			return documentoPago;
		}
		
		ClpmDocumentoPagoEmpresa documentoPago = new ClpmDocumentoPagoEmpresa();
		ClpbClienteEmpresa clpbClienteEmpresa = new ClpbClienteEmpresa();
		documentoPago.setClpbClienteEmpresa(clpbClienteEmpresa);
		documentoPago.setFechaPagoFactura(CalendarUtil.calendarToString(CLPConstantes.DATE_PATTERN, Calendar.getInstance()));
		documentoPago.setFechaRegistroPagoFactura(CalendarUtil.calendarToString(CLPConstantes.DATE_PATTERN, Calendar.getInstance()));
		documentoPago.setFechaEmisionFactura(CalendarUtil.calendarToString(CLPConstantes.DATE_PATTERN, Calendar.getInstance()));
		return documentoPago;
	}
	
	@RequestMapping(value = "/documentopagoform.html", method = RequestMethod.POST)
	public String actualizarDocumentoPago(@ModelAttribute("documentoPago") 
			ClpmDocumentoPagoEmpresa documentoPago, BindingResult result, SessionStatus status, 
			HttpServletRequest request) {
		
		try {
			
			String fechaEmision = documentoPago.getFechaEmisionFactura();
			String fechaPagoFactura = documentoPago.getFechaPagoFactura();
			Calendar calEmision = CalendarUtil.stringToCalendar(CLPConstantes.DATE_PATTERN, fechaEmision);
			Calendar calPagoFactura = CalendarUtil.stringToCalendar(CLPConstantes.DATE_PATTERN, fechaPagoFactura);
			documentoPago.setDaFechaEmisionFactura(calEmision);
			documentoPago.setDaFechaPagoFactura(calPagoFactura);		
			
			documentoPagoEmpresaService.guardarDocumentoPagoEmpresa(documentoPago);
						
			saveMessage(request, getText("documentopagoform.added",request.getLocale()));
			return "redirect:documentopagolist.html";
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			request.setAttribute("documentoPago",documentoPago);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/documentopago/documentopagoform";
		}
	}	
	
	@RequestMapping(value = "/clientefordocumentopago.html", method = RequestMethod.GET)
	public String  listarClientes(
			Model model) {
			List<BNClienteEmpresa> clientes = clienteEmpresaService.obtenerClienteEmpresasConLineaCredito(Boolean.FALSE);
			model.addAttribute("clientes", clientes);
		return "/wizard/clientedocumentopagowizzardform";
	}
}
